<template>
  <div class="app-container">
    <el-card>
      <el-form :model="addForm" :rules="rules" ref="addForm" label-width="100px" size="small">
        <el-form-item label="分类:" prop="codeId">
          <el-select style="width: 500px" v-model="addForm.codeId" placeholder="请选择分类">
            <el-option v-for="(item, index) in cateList" :key="index" :label="item.label"
              :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="标题:">
          <el-input v-model="addForm.title" maxlength="50" style="width: 500px"></el-input>
        </el-form-item>
        <el-form-item label="图片:" prop="imageUrl">
          <single-upload uploadType="2" :maxCount="1" v-model="addForm.imageUrl" :width="'100px'" :height="'100px'"
            :border="'1px solid #ccc'">
            <div class="uploadPic2" v-if="!addForm.imageUrl">
              <i style="font-size: 40px; color: #ccc; font-weight: bold" class="el-icon-plus"></i>
            </div>
          </single-upload>
        </el-form-item>
        <el-form-item label="发布平台:" prop="platformType">
          <el-checkbox-group v-model="addForm.platformType">
            <el-checkbox v-for="city in platformTypeList" :label="city.dictValue" :key="city.dictValue">{{
              city.dictLabel }}</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item>
          <el-button v-debounce :disabled="isDetail" type="primary" @click="onSubmit('addForm')">确定</el-button>
          <el-button @click="resetForm('addForm')">取消</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { addToolLoans, getToolLoans, editToolLoans } from "@/api/tool";
export default {
  components: {
    SingleUpload: () => import("@/components/Upload/singleUpload"),
  },
  async created() {
    this.platformTypeList = await this.$getDictData("platform_type");
    this.cateList = await this.$getDictData("poster");

    if (this.$route.query.id) {
      this.$route.meta.title = "编辑海报";
      this.getInfo();
    }
    if (this.$route.query.id && this.$route.query.type == 'detail') {
      this.$route.meta.title = "查看海报";
      this.isDetail = true
      this.getInfo();
    }
  },
  data() {
    return {
      addForm: {
        codeId: "",
        title: "",
        imageUrl: "",
        platformType: ['2'],
      },
      rules: {
        codeId: [{ required: true, message: "请选择分类", trigger: "blur" }],
        title: [{ required: true, message: "请输入标题", trigger: "blur" }],
        imageUrl: [{ required: true, message: "请上传图片", trigger: "blur" }],
        platformType: [
          { required: true, message: "请选择发布平台", trigger: "blur" },
        ],
      },
      cateList: [],
      platformTypeList: [],
      isDetail: false
    };
  },
  methods: {
    getInfo() {
      getToolLoans(this.$route.query.id).then((res) => {
        res.data.imageUrl = res.data.imageUrl.split(",");
        res.data.platformType = res.data.platformType.split(",");
        this.addForm = Object.assign({}, res.data);
      });
    },
    onSubmit(form) {
      this.$refs[form].validate((valid) => {
        if (valid) {
          let data = {
            codeId: this.addForm.codeId,
            title: this.addForm.title,
            imageUrl: this.addForm.imageUrl.join(","),
            platformType: this.addForm.platformType.join(","),
            toolType: "poster",
          };

          if (this.addForm.id) {
            data.id = this.addForm.id;
            editToolLoans(data).then((res) => {
              this.$message({
                message: "修改成功",
                type: "success",
              });
              this.$router.go(-1);
            });
          } else {
            addToolLoans(data).then((res) => {
              this.$message({
                message: "添加成功",
                type: "success",
              });
              this.$router.go(-1);
            });
          }
        }
      });
    },
  },
};
</script>

<style></style>
